﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using System.Collections;
using DevExpress.Xpo.Metadata;
using Microsoft.Practices.Unity;
using iAgent.Core;
namespace iAgent
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            InitDataLayer();
            InitProviders();
#if DEBUG
            //using (var x = new UnitOfWork())
            //{
            //    DataModel.Models.Person pers = new DataModel.Models.Person(x) { LastName = "Иванов", FirstName = "Иван", PatronymicName = "Иванович", BirthDate = DateTime.Now }; ;
            //    pers.DriverCard = new DataModel.Models.DriverCard() { Number = "123", Serial = "3214", DateOut = DateTime.Now };
            //    Random rnd = new Random(1);
            //    DataModel.Models.InsuranceCompany company = new DataModel.Models.InsuranceCompany(x) { Name = "Test" };
            //    for (int i = 0; i < 1000; i++)
            //    {
            //        DataModel.Models.Person tp = new DataModel.Models.Person(x) { LastName = rnd.Next(100).ToString(), FirstName = rnd.Next(100).ToString(), PatronymicName = rnd.Next(100).ToString(), BirthDate = DateTime.Now };
            //        DataModel.Models.PersonClient PC = new DataModel.Models.PersonClient(x) { Person = tp };
            //        DataModel.Models.InsurancePolicy pol = new DataModel.Models.InsurancePolicy(x) { Number = "123", Client = PC, InsuranceCompany = company, Bonus = new DataModel.Models.Money() { Value = 150, Currency = DataModel.Models.Currency.Dollar } };
            //    }
            //    //DataModel.Models.Person pers = new DataModel.Models.Person(x) { LastName = "Иванов", FirstName = "Иван", PatronymicName = "Иванович", BirthDate = DateTime.Now }; ;
            //    //pers.DriverCard = new DataModel.Models.DriverCard() { Number = "123", Serial = "3214", DateOut = DateTime.Now };                    
            //    //DataModel.Models.PersonClient PC = new DataModel.Models.PersonClient(x) { Person = pers };
            //    //DataModel.Models.PersonClient PC2 = new DataModel.Models.PersonClient(x) { Person = pers };
            //    //DataModel.Models.CompanyClient CC = new DataModel.Models.CompanyClient(x) { Name = "Company Z" };
            //    //DataModel.Models.Blank blank = DataModel.Models.Factories.BlankFactory.CreateBlank(x);
            //    //DataModel.Models.InsuranceCompany company = new DataModel.Models.InsuranceCompany(x) { Name = "Test" };
            //    //DataModel.Models.InsurancePolicy pol = new DataModel.Models.InsurancePolicy(x) { Number = "123", Client = PC, InsuranceCompany = company, Bonus = new DataModel.Models.Money() { Value = 150, Currency = DataModel.Models.Currency.Dollar } };
            //    //DataModel.Models.Payment p = new DataModel.Models.Payment(x) { Summa = new DataModel.Models.Money() { Value = 100, Currency = DataModel.Models.Currency.Euro }, Policy = pol };
            //    x.CommitChanges();
            //}
            
#endif
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmMain());
        }

        private static void InitDataLayer()
        {
            //string conn = MSSqlCEConnectionProvider.GetConnectionString("DataBase.sdf");
            //XpoDefault.DataLayer = XpoDefault.GetDataLayer(conn, AutoCreateOption.SchemaOnly);
            string ConnectinoString = MSSqlCEConnectionProvider.GetConnectionString("DataBase.sdf");
            IDataStore DataStore = XpoDefault.GetConnectionProvider(ConnectinoString, AutoCreateOption.SchemaOnly);
            DataCacheRoot Root = new DataCacheRoot(DataStore);
            DataCacheNode Cache = new DataCacheNode(Root);
            XpoDefault.DataLayer = new SimpleDataLayer(Cache);  
            //DevExpress.Xpo.Logger.Transport.LogServer server = new DevExpress.Xpo.Logger.Transport.LogServer(12345);
            //DevExpress.Xpo.Logger.LogManager.SetTransport(server);
            XpoDefault.Session = null;
        }
        private static void InitProviders()
        {
            DataModel.Core.Settings settings = new DataModel.Core.Settings();
            UnityContainer container = new UnityContainer();
            settings.IOC = new IOCHelper(container);
            settings.UserSettings = new UserSettings();
            container.RegisterType<IFileHelper, Core.FileRepositoryDecorator>(new ContainerControlledLifetimeManager());
            DataModel.Core.Settings.Current = settings;
        }
    }
}
